<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js模块化之按需加载</title>
</head>
<body>
  <ul>
    <li>使用import导入模块，有时可能不是页面初始化就加载，而是引发某个操作时才加载, 以提升性能</li>
    <li>使用import()函数按需动态加载，返回promise, 加载成功后返回当前模块</li>
  </ul>
  <button name="load">按需加载</button>
  <script>
    document.querySelector("[name='load']").addEventListener('click', () => {
      import('./user.js').then(user => {
        console.log(user.name)  // Miracle
        console.log(user.web)   // https://github.com/miracle-git
        user.show()             // welcome to visit Miracle's git: https://github.com/miracle-git
      })
    })
  </script>
</body>
</html>